package com.kelvem.saas.workbanch.ontology.carrier.service;

import com.kelvem.saas.workbanch.core.base.BaseCriteria;
import com.kelvem.saas.workbanch.core.base.BaseService;
import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.core.exception.SaasException;
import com.kelvem.saas.workbanch.core.utils.DateUtil;
import com.kelvem.saas.workbanch.fsm.dao.BaseDao;
import com.kelvem.saas.workbanch.ontology.carrier.dao.LineOfferDao;
import com.kelvem.saas.workbanch.ontology.carrier.model.LineOfferEntity;
import com.kelvem.saas.workbanch.ontology.carrier.request.LineOfferQueryVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线路报价Service
 *
 * @author kelvem
 * @version 1.0
 */
@Service
@Slf4j
public class LineOfferService extends BaseService<LineOfferEntity, LineOfferQueryVo> {
    
    @Resource
    private LineOfferDao lineOfferDao;
    
    /**
     * 增加线路报价
     *
     * @param lineOffer 线路报价信息
     * @return Long 主键
     */
    @Transactional(rollbackFor = Exception.class)
    public Long addLineOffer(LineOfferEntity lineOffer) {
        
        log.info("LineOfferService:addLineOffer:start, lineOffer=【{}】", lineOffer);
        SaasAssert.notNull(lineOffer);
        
        try {
            if (lineOffer.getCreateTime() == null) {
                lineOffer.setCreateTime(DateUtil.now());
            }
            if (lineOffer.getUpdateTime() == null) {
                lineOffer.setUpdateTime(DateUtil.now());
            }
            
            lineOfferDao.insert(lineOffer);
            Long lineOfferId = lineOffer.getId();
            log.info("LineOfferService:addLineOffer:end, lineOfferId={}", lineOfferId);
            
            lineOfferDao.insert(lineOffer);
            return lineOffer.getId();
        } catch (Exception e) {
            String msg = String.format("LineOfferService:addLineOffer:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 修改线路报价
     *
     * @param lineOffer 线路报价信息
     * @return 更新记录数
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateLineOffer(LineOfferEntity lineOffer) {
        
        log.info("LineOfferService:updateLineOffer:start, lineOffer=【{}】", lineOffer);
        SaasAssert.notNull(lineOffer);
        SaasAssert.notNull(lineOffer.getId());
        
        try {
            int count = lineOfferDao.updateById(lineOffer);
            log.info("LineOfferService:updateLineOffer:end, count={}", count);
            return count;
        } catch (Exception e) {
            String msg = String.format("LineOfferService:updateLineOffer:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 删除线路报价
     *
     * @param lineOfferId 线路报价id
     * @return 删除记录数
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteLineOffer(Long lineOfferId) {
        
        log.info("LineOfferService:deleteLineOffer:start, lineOfferId={}", lineOfferId);
        SaasAssert.notNull(lineOfferId);
        
        try {
            int count = lineOfferDao.deleteById(lineOfferId);
            log.info("LineOfferService:deleteLineOffer:end, count={}", count);
            return count;
        } catch (Exception e) {
            String msg = String.format("LineOfferService:deleteLineOffer:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 查询线路报价分页数据
     *
     * @param lineOfferQueryVo 查询用
     * @return Page<LineOfferDto>
     */
    public Page<LineOfferEntity> queryLineOfferPage(LineOfferQueryVo lineOfferQueryVo) {
        
        log.info("LineOfferService:queryLineOfferPage:start, lineOfferQueryVo=【{}】", lineOfferQueryVo);
        SaasAssert.notNull(lineOfferQueryVo);
        
        try {
            Page<LineOfferEntity> page = super.queryPage(lineOfferQueryVo);
            log.info("LineOfferService:queryLineOfferPage:end, page.size={}", page.getRecords().size());
            return page;
        } catch (Exception e) {
            String msg = String.format("LineOfferService:queryLineOfferPage:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 查询线路报价列表数据
     *
     * @param lineOfferQueryVo 查询用
     * @return List<LineOffer>
     */
    public List<LineOfferEntity> queryLineOfferList(LineOfferQueryVo lineOfferQueryVo) {
        
        log.info("LineOfferService:queryLineOfferList:start, lineOfferQueryVo=【{}】", lineOfferQueryVo);
        SaasAssert.notNull(lineOfferQueryVo);
        
        try {
            List<LineOfferEntity> list = super.queryList(lineOfferQueryVo);
            log.info("LineOfferService:queryLineOfferList:end, list.size={}", list.size());
            return list;
        } catch (Exception e) {
            String msg = String.format("LineOfferService:queryLineOfferList:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 根据id查询线路报价信息
     *
     * @param lineOfferId 线路报价id
     * @return LineOffer
     */
    public LineOfferEntity getLineOfferById(Long lineOfferId) {
        
        log.info("LineOfferService:getLineOfferById:start, lineOfferId={}", lineOfferId);
        SaasAssert.notNull(lineOfferId);
        
        try {
            LineOfferEntity lineOffer = lineOfferDao.selectById(lineOfferId);
            log.info("LineOfferService:getLineOfferById:end, lineOfferInfo=【{}】", lineOffer);
            return lineOffer;
        } catch (Exception e) {
            String msg = String.format("LineOfferService:getLineOfferById:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 查询线路报价列表数据
     *
     * @param province 省
     * @param city     市
     * @param county   县
     * @return LineOffer
     */
    public LineOfferEntity getLineOffer(String province, String city, String county) {
        
        log.info("LineOfferService:getLineOffer:start, province=【{}】, city=【{}】, county=【{}】", province, city, county);
        SaasAssert.notNull(province);
        SaasAssert.notNull(city);
        SaasAssert.notNull(county);
        
        try {
            LineOfferQueryVo lineOfferQueryVo = new LineOfferQueryVo();
            lineOfferQueryVo.setProvince(province);
            lineOfferQueryVo.setCity(city);
            lineOfferQueryVo.setCounty(county);
            List<LineOfferEntity> list = super.queryList(lineOfferQueryVo);
            log.info("LineOfferService:getLineOffer:end, list.size={}", list.size());
            
            if (!list.isEmpty()) {
                return list.get(0);
            } else {
                return null;
            }
        } catch (Exception e) {
            String msg = String.format("LineOfferService:getLineOffer:error, %s", e.getMessage());
            log.error(msg, e);
            throw new SaasException(msg);
        }
    }
    
    /**
     * 构建查询用Criteria
     * 基类的queryPage, queryList方法使用
     *
     * @param queryVo 查询用
     * @return BaseCriteria
     */
    @Override
    public BaseCriteria<LineOfferEntity> buildQueryCriteria(LineOfferQueryVo queryVo) {
        BaseCriteria<LineOfferEntity> criteria = new BaseCriteria<>();

        // 主键
        Long id = queryVo.getId();
        if (super.isNotEmpty(id)) {
            criteria.andEqualTo("id", queryVo.getId());
        }
        
        // 创建时间
        Date startCreateTime = queryVo.getStartCreateTime();
        if(super.isNotEmpty(startCreateTime))
        {
            criteria.andGreaterThanOrEqualTo("createTime", startCreateTime);
        }
        Date endCreateTime = queryVo.getEndCreateTime();
        if(super.isNotEmpty(endCreateTime))
        {
            criteria.andLessThanOrEqualTo("createTime", endCreateTime);
        }
        
        // 省
        String province = queryVo.getProvince();
        if (super.isNotEmpty(province)) {
            criteria.andEqualTo("province", province);
        }
        
        // 市
        String city = queryVo.getCity();
        if (super.isNotEmpty(city)) {
            criteria.andEqualTo("city", city);
        }
        
        // 县
        String county = queryVo.getCounty();
        if (super.isNotEmpty(county)) {
            criteria.andEqualTo("county", county);
        }
        
        // 创建人
        String createUid = queryVo.getCreateUid();
        if (super.isNotEmpty(createUid)) {
            criteria.andEqualTo("optUid", createUid);
        }
        
        // 更信人
        String updateUid = queryVo.getUpdateUid();
        if (super.isNotEmpty(updateUid)) {
            criteria.andEqualTo("updateUid", updateUid);
        }
        
        return criteria;
    }

    @Override
    public BaseDao<LineOfferEntity> getDao() {
        return this.lineOfferDao;
    }
}
